<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 合并两个链表有点类似于两个链表相加，一般双链表题目都是通过while(list1&&list2)然后再判断两个链表是否存在 */
            var mergeTwoLists = function (list1, list2) {
                let dummy = new ListNode(0, null)
                let p = dummy
                while (list1 && list2) {
                    if (list1.val <= list2.val) {
                        // let node = new ListNode(list1.val)
                        /* 其实不需要创建节点 */
                        p.next = list1
                        list1 = list1.next
                        p = p.next
                    } else {
                        p.next = list2
                        list2 = list2.next
                        p = p.next
                    }
                }
                if (list1) {
                    p.next = list1
                }
                if (list2) {
                    p.next = list2
                }
                return dummy.next
            }
        </script>
    </body>
</html>
